package com.jeewms.www.tms.ui.acitivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jeewms.www.tms.R;
import com.jeewms.www.tms.base.BaseBackActivity;
import com.jeewms.www.tms.bean.vm.Ginfo;
import com.jeewms.www.tms.bean.vm.wmapiUtil.ResultDO;
import com.jeewms.www.tms.constance.Constance;
import com.jeewms.www.tms.util.EditChangedListener;
import com.jeewms.www.tms.util.LoadingUtil;
import com.jeewms.www.tms.util.SharedPreferencesUtil;
import com.jeewms.www.tms.util.z_toast;
import com.jeewms.www.tms.util.GsonUtils;
import com.jeewms.www.tms.util.Logutil;
import com.jeewms.www.tms.util.UUIDUtil;
import com.jeewms.www.tms.volley.HTTPUtils;
import com.jeewms.www.tms.volley.VolleyListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KcTransferActivity extends BaseBackActivity {

    private TextView btn = null;
    private TextView tv_btnbc=null;
    private long lastClickTime = 0L;
    private static final int FAST_CLICK_DELAY_TIME = 1000;  // 快速点击间隔
    String tag ;
    ListView mLvGent;
    ListView trlistview;
    List<Ginfo> mginfo;
    KcTransferActivity.GINfo madaper;
    GINfo MyAdapter;
    private int titlebar_back;
    public static void show(Context context) {
        Intent intent = new Intent(context, KcTransferActivity.class);
        context.startActivity(intent);

    }

    protected void initView() {
        super.initView();

        Logutil.print("测试System的使用LOGCAT");
        setTitle("转储");
        final EditText et_fangweima = (EditText) findViewById(R.id.et_fangweima);
        final EditText et_chuwei = (EditText) findViewById(R.id.et_chuwei);
      //  et_chuwei.addTextChangedListener(new EditChangedListener(et_chuwei));
       // et_fangweima.addTextChangedListener(new EditChangedListener(et_fangweima));
        final EditText tv_werks = findViewById(R.id.et_werks);//工厂
        final EditText et_werks = (EditText) findViewById(R.id.et_werks);
        final EditText et_lgort = (EditText) findViewById(R.id.et_lgort);
       // et_lgort.addTextChangedListener(new EditChangedListener(et_lgort));
        btn = (TextView) findViewById(R.id.tv_search);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if(System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME){
                   Logutil.print("多次速度调用接口");
                   return;
               }
                lastClickTime = System.currentTimeMillis();
                z_toast ts = new z_toast();

                if (TextUtils.isEmpty(et_werks.getText().toString())) {
                    ts.showToast("工厂不能为空！",KcTransferActivity.this);
                    return;
                }
                if (TextUtils.isEmpty(et_lgort.getText().toString())) {
                    ts.showToast("库位不能为空！",KcTransferActivity.this);
                    return;
                }
                getData();
            }
        });

     //  Logutil.print("执行出问题");
    }
    private void getData() {
//        String url = Constance.gettSapLsqdControllerURL() + "?username=" + SharedPreferencesUtil.getInstance(this).getKeyValue(Constance.SHAREP.LOGINNAME);
//        if (!StringUtil.isEmpty(etJiaojiedan.getText().toString()))
//            url += "&searchstr=" + etJiaojiedan.getText().toString();
//        if (!StringUtil.isEmpty(etFangweima.getText().toString()))
//            url += "&searchstr2=" + etFangweima.getText().toString();

       // String url = "http://192.168.0.108:8080/zzwms/rest/wmapi/getdoc/KCCX";
       //  String url = "http://172.17.220.244:8080/zzwms/rest/wmapi/getdoc/KCCX";
    //   String url = "http://192.168.200.100/zzwms/rest/wmapi/getdoc/KCCX";
        final EditText et_fangweima1 = (EditText) findViewById(R.id.et_fangweima);
        final EditText et_werks1 = (EditText) findViewById(R.id.et_werks);
        final EditText et_lgort1 = (EditText) findViewById(R.id.et_lgort);
        final EditText et_chuwei1 = (EditText) findViewById(R.id.et_chuwei);
        // String url = "http://172.17.210.197:8080/zzwms/rest/wmapi/getdoc/KCCX";
        String url = "http://192.168.200.100/zzwms/rest/wmapi/getdoc/KCCX"+"?searchstr1="
                +"&searchstr2="  + et_fangweima1.getText().toString()  +"&searchstr3="  + et_chuwei1.getText().toString()  +"&searchstr4="  + et_werks1.getText().toString()
                +"&searchstr5="  + et_lgort1.getText().toString();
        Logutil.print("url" + url);
        HTTPUtils.get(getActivity(), url, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }

            @Override
            public void onResponse(String response) {
                Logutil.print("res==" + response);
                ResultDO vm = GsonUtils.parseJSON(response, ResultDO.class);
                mginfo = new ArrayList<Ginfo>();
                // Logutil.print("mginfo.size()1=");
                for(int i = 0;i<vm.getObj().size();i++) {
                    Ginfo bean = new Ginfo();
                    bean.setResid(vm.getObj().get(i).getWmX2());
                       bean.setMenge(vm.getObj().get(i).getWmX3());
                 //   bean.setMenge(Integer.parseInt(vm.getObj().get(i).getWmX3()));
                    bean.setBinId(vm.getObj().get(i).getWmX6());
                    bean.setMaktx(vm.getObj().get(i).getWmX8());
                    bean.setPc(vm.getObj().get(i).getWmX7()); //增加批次
                    mginfo.add(bean);
                    //    Logutil.print("mginfo.size()2="+mginfo.size());
                }
                initview();

                //    Logutil.print("mginfo.size()3="+mginfo.size());

            }
        });

    }

    @SuppressLint("WrongViewCast")
    private void initview() {
        mLvGent = (ListView)findViewById(R.id.trlistview);
       madaper = new GINfo();
        mLvGent.setAdapter(madaper);

    }
    class GINfo extends BaseAdapter {
        @Override
        public int getCount() {
            return mginfo.size();
        }

        @Override
        public Ginfo getItem(int i) {

            return mginfo.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            //---------------------------------------------------------------------------------------------------------变量定义
            final int k = 0;
            final View layout = View.inflate(KcTransferActivity.this, R.layout.item_kctr,null); //取ITEM的值
            final View layout2 = View.inflate(KcTransferActivity.this, R.layout.activity_kc_transfer,null);//取表头的值
            //---------------------------------------------------------------------------------------------------------变量定义


          //------------------------------------------------------------------------------------------------------------ ----从页面能获取数据
           TextView tv_btnbc = layout.findViewById(R.id.tv_btnbc);// 设置LISTVIEW监听按钮

           tv_btnbc.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                    TextView  tv_matnr = layout.findViewById(R.id.tv_matnr);//移仓物料号
                    final EditText tv_mbcw = layout.findViewById(R.id.et_mbcw);  //目标仓位
                   tv_mbcw.addTextChangedListener(new EditChangedListener(tv_mbcw));
                    final EditText tv_mbsl = layout.findViewById(R.id.et_mbsl);//转移数量
                    final EditText tv_werks = (EditText) findViewById(R.id.et_werks);//工厂
                    final EditText tv_lgort = (EditText)findViewById(R.id.et_lgort);//库位
                   final TextView tv_menge = layout.findViewById(R.id.tv_menge);//库存数量
              //  TextView   tv = (TextView)findViewById(R.id.et_werks);
                    final TextView tv_bindid = layout.findViewById(R.id.tv_binId);//原始仓位
                    final TextView tv_pc = layout.findViewById(R.id.tv_pc) ;//批次
                   z_toast ts = new z_toast();

                   if (TextUtils.isEmpty(tv_mbsl.getText())){
                       z_toast t_dm = new z_toast();
                       t_dm.showToast("转储数量不能为空",KcTransferActivity.this);
                       LoadingUtil.hideLoading();
                       return;
                   }

                   if (TextUtils.isEmpty(tv_mbcw.getText())){
                       z_toast t_dm = new z_toast();
                       t_dm.showToast("目标仓位不能为空",KcTransferActivity.this);
                       LoadingUtil.hideLoading();
                       return;
                   }


//                   //点击保存按钮 检查主要数据是否为空，为空Toast 不能继续.
                   if (!TextUtils.isEmpty(tv_mbcw.getText())) {
                       if (Double.parseDouble(tv_menge.getText().toString()) < Double.parseDouble(tv_mbsl.getText().toString())) {
                           z_toast t_dm = new z_toast();
                           t_dm.showToast("转储数量大于库存数量!", KcTransferActivity.this);
                           LoadingUtil.hideLoading();
                           return;
                       }
                   }


                   if (TextUtils.isEmpty(tv_lgort.getText().toString())) {

                       ts.showToast("库位不能为空",KcTransferActivity.this);
                       return;
                   }




                   if (TextUtils.isEmpty(tv_lgort.getText().toString())) {

                       ts.showToast("库位不能为空",KcTransferActivity.this);
                       return;
                   }
                   if (TextUtils.isEmpty(tv_werks.getText().toString())) {
                          ts.showToast("工厂不能为空",KcTransferActivity.this);

                       return;
                   }
                   if (TextUtils.isEmpty(tv_mbcw.getText().toString())) {
                       ts.showToast("目标仓位不能为空",KcTransferActivity.this);
                       return;
                   }
                   if (TextUtils.isEmpty(tv_mbsl.getText().toString())) {
                       ts.showToast("目标数量不能为空",KcTransferActivity.this);
                       return;
                   }

           //-------------------------------------------------------------------------------------------------------------调用接口，给接口JSon赋值
                   Map<String, String> map = new HashMap<>();
                //  map.put("createBy",SharedPreferencesUtil.getInstance(mContext).getKeyValue(Constance.SHAREP.LOGINNAME));
                   map.put("createBy", SharedPreferencesUtil.getInstance(getActivity()).getKeyValue(Constance.SHAREP.LOGINNAME));
                   map.put("wxs1", UUIDUtil.getUUID32());//流水唯一ID号
                   map.put("wxs4", tv_matnr.getText().toString());//物料号
                   map.put("wxs5", tv_werks.getText().toString());//工厂
                   map.put("wxs6", tv_lgort.getText().toString());//库位
                 //  map.put("wxs7", tv_mbcw.getText().toString());//目标仓位
                   map.put("wxs8", tv_mbsl.getText().toString());//转移数量
                   map.put("wxs9", tv_mbcw.getText().toString());//目标仓位
                   map.put("wxs7", tv_bindid.getText().toString());//原始仓位
                   map.put("wxs31",tv_pc.getText().toString());   //设置批次

                 //map.put("x2", SharedPreferencesUtil.getInstance(mContext).getKeyValue(Constance.SHAREP.LOGINNAME)); //登录用户名
                   final JSONObject jsonObject = new JSONObject(map);
                   final Map<String, String> params = new HashMap<>();
                   params.put("tSapLtttstr", jsonObject.toString());//上传实体json

                  // String url = "http://192.168.200.100:8080/zzwms/rest/wmapi/docltcreate"+ "/YK";
               //    String Zurl = "http://172.17.210.196:8080/zzwms/rest/wmapi/docltcreate?doctype=YK&tSapLtttstr="+ params.put("tSapLtttstr", jsonObject.toString())+"";
                   String Zurl = "http://192.168.200.100/zzwms/rest/wmapi/docltcreate?doctype=YK&tSapLtttstr="+ params.put("tSapLtttstr", jsonObject.toString())+"";
                   Logutil.print("url" + Zurl);
                   ts = new z_toast();
                   ts.showToast("保存中，请稍后。。。。",KcTransferActivity.this);
                   z_toast  tk = new z_toast();
                   HTTPUtils.get(getActivity(), Zurl, new VolleyListener() {

                       public void onErrorResponse(VolleyError error) {
                           z_toast  tp = new z_toast();
                           getData();
                           tp.showToast("jeecg数据为空",KcTransferActivity.this);
                       }

                       @Override
                       public void onResponse(String response) {
                           // -----------------------------------------------------------------------------------------取json 返回数据并显示
                           ResultDO result = GsonUtils.parseJSON(response, ResultDO.class);
                           z_toast  tk = new z_toast();
                           tk.showToast(result.getErrorMsg(),KcTransferActivity.this);
                           try {
                               Thread.sleep(1000);
                           } catch (InterruptedException e) {
                               e.printStackTrace();
                           }
                           //重新调用库存接口 刷新页面，取得实际库存.
                           getData();

                           //重新调用库存接口 刷新页面，取得实际库存.

                       }
                   });

               }
           });

            //多行ITEM的显示，只要在实体类GINfo,java 增加变量,在这里增加变量参数
            TextView tvmatnr = layout.findViewById(R.id.tv_matnr);
            TextView tvmenge = layout.findViewById(R.id.tv_menge);
            TextView tvmaktx = layout.findViewById(R.id.tv_maktx);
            TextView tvbinId = layout.findViewById(R.id.tv_binId);
            TextView tv_pc = layout.findViewById(R.id.tv_pc);
          //  Logutil.print(tvmatnr.getText().toString());
            Ginfo bean = mginfo.get(i);
            tvmatnr.setText(bean.getResid());
           // tvmenge.setText(Integer.toString(bean.getMenge()));
            tvmenge.setText(bean.getMenge());
            tvmaktx.setText(bean.getMaktx());
            tvbinId.setText(bean.getBinId());
            tv_pc.setText(bean.getPc());
            return layout;
        }
    }

    private void parseDdta(String  result) throws JSONException {

        JSONObject JSONobject = new JSONObject(result);
        JSONArray JSONArray = JSONobject.getJSONArray("list");

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    protected int getContentResId() {
        return R.layout.activity_kc_transfer;
    }
}
